# d ;


*label variables to mathc the names in the tables ;
label var exp_L_IH_SF12a_r "\( Y^L \)";
label var  exp_IRI_IH_SF12a_r "\( Y^L\:in\:IRI \)";
label var  exp_IH_mtn_SF12a_r "\( Y^L\: in\: Maint.\)";

****************************************************;
*calculate differences in miles and IRI;
*local D_vars "u_iri_IH u_iri_urban u_iri_rural u_lane_miles_IH u_lane_miles_rural u_lane_miles_urban";
local D_vars iri ;
sort state id year ;
foreach varname of local D_vars{;
	*changed from leads to lags, 20190429;
	gen D`varname'=cond(
			id[_n]==id[_n-1]&
			state[_n]==state[_n-1]&
			`varname'[_n]!=.&`varname'[_n-1]!=.&
			`varname'[_n]!=0&`varname'[_n-1]!=0&
			year[_n]==year[_n-1]+1,
			`varname'[_n]-`varname'[_n-1],.);
	gen lag_`varname'=cond(
			id[_n]==id[_n-1]&
			state[_n]==state[_n-1]&
			`varname'[_n]!=.&`varname'[_n-1]!=.&
			`varname'[_n]!=0&`varname'[_n-1]!=0&
			year[_n]==year[_n-1]+1,
			`varname'[_n-1],.);		
			};

sum Diri d_iri;	
drop d_iri;

** indicator for states with maintenance reported in HPMS;
*bysort state year : egen D_exp_I2=max(I2_t) if I2_t!=.; 
*label var D_exp_I2 "Dummy=1 for state year if if at least one sample id reports maintenance";
	
*keep if _n<50;
*Total lane miles IH in each state-year; 
bysort state year : egen lane_miles_st=total(lane_miles);
label var  lane_miles_st "Total IH lane miles in state year (HPMS-sample)" ;
*Total lane miles resurfaced IH in each state-year; 
gen lane_miles_x=I2_t*lane_miles;
bysort state year : egen lane_milesI2_st=total(lane_miles_x);
drop lane_miles_x;
label var  lane_milesI2_st "Total I2 IH lane miles in state year (HPMS-sample)" ;
*keep year state lane_miles lane_miles_st lane_milesI2_st I2_t;
*order year state lane_miles lane_miles_st lane_milesI2_st I2_t;

* Real IH Expenditure per IH lane mile;
gen app_IH_plm=			(app_IH_r/lane_miles_st );
gen exp_IRI_IH_SF12a_plm=	(exp_IRI_IH_SF12a_r/lane_miles_st );
gen exp_L_IH_SF12a_plm=		(exp_L_IH_SF12a_r/lane_miles_st );
label var app_IH_plm 		"Appropriations per lane mile" ;
label var exp_IRI_IH_SF12a_plm 	"Exp. Resurfacing per lane mile" ;
label var exp_L_IH_SF12a_plm 	"Exp. construction per lane mile" ;

* Real IH Expenditure per resurfaced IH lane mile;
gen app_IH_I2_plm=			(app_IH_r/lane_milesI2_st );

gen exp_IRI_IH_I2_SF12a_plm=		(exp_IRI_IH_SF12a_r/lane_milesI2_st );
gen exp_L_IH_I2_SF12a_plm=		(exp_L_IH_SF12a_r/lane_milesI2_st );
label var app_IH_I2_plm 		"Appropriations per resurf.lane m." ;
label var exp_IRI_IH_I2_SF12a_plm 	"Exp. Resurfacing per resurf. lane m." ;
label var exp_L_IH_I2_SF12a_plm 	"Exp. Construction per resurf.lane m." ;


forvalues i=1(1)5{;
		
		gen L`i'FHWA_apport_LW_r_plm=			(L`i'FHWA_apport_LW_r/lane_milesI2_st );

};	

tab state year if Diri==.;

*drop state-years with no resurfacing expenditure;
drop if exp_IRI_IH_SF12a_r==0;
drop if exp_IRI_IH_SF12a_r==.;
*drop id-years with bad IRI data;
drop if Diri==.;
drop if abs(Diri)>200;
*drop state-years with no resurfacing;
drop if lane_milesI2_st==.|lane_milesI2_st==0;
*drop DC and years outside sample;
drop if state==11;
drop if year<1992;
drop if year>2008;
tab state;

*aadt variables;	
gen aadt2_10000=aadt_1000^2;
label var aadt2_10000 "(`:  var label aadt_1000')^2" ;
gen aadt3_10000=aadt_1000^3;
label var aadt3_10000 "(`:  var label aadt_1000')^3" ;
	
*Set up state year and I2 x year FE;
*calculate number of years that each state is present in data;
preserve;
	sort state year;
	by state year: gen counter1=_n;
	keep if counter1==1;
	display "count number of stateXyears for reference";
	sum year; 
	sort state;
	by state: gen num_years=_N;
	by state: gen counter3=_n;
	keep if counter3==1;
	label var num_years "Years state present in data";
	keep state num_years;
	tab num_years;
	display "states present 1990-2008 --- use Texas 1990 for omitted state-year";
	tab state if num_years==18;
	tab state if num_years==18, nol;// Texas, fips 48 is present in all years
	sort state;
	save stateyears,replace;
restore;

merge m:1 state using stateyears;
tab _merge;
order state state_name year num_years;
erase stateyears.dta;

*make state-year indicators for state years present in data;	
levelsof year, local(year_list);
levelsof state, local(state_list);
display "`state_list'";
display "`year_list'";
local i_stateyear=1;
foreach k_year of local year_list{;
	foreach k_state of local state_list{;
		if !("`k_year'"=="1990"&"`k_state'"=="48"){; //texas 1990 omitted
			*make dummy for state-year;
			gen stateXyearI_`k_year'_`k_state'=cond(year==`k_year'&state==`k_state',1,0);
			*drop indicator if state-year missing from data;
			quiet sum stateXyearI_`k_year'_`k_state';
			local tester =r(mean) ;
			if `tester'==0{;
				drop stateXyearI_`k_year'_`k_state';
				};
			*increment counter if state-year present in data. counter is just used as a diagnostic;
			if `tester'>0{;
				local ++i_stateyear;
				};
			};//end skip texas 1990 if
		};//end state loop
	};//end year loop
display "`i_stateyear'-1 state-year indicators created -- should match tab above";

*make year X I2_t indicators and year X I2_t * expenditure per mile variables;	
levelsof year, local(year_list);
display "`year_list'";
foreach k_year of local year_list{;
	gen I2_t_x`k_year'			=cond(year==`k_year'& I2_t==1,1,0);
	gen I2_t_`k_year'_exp_I2	=I2_t_x`k_year'*exp_IRI_IH_SF12a_plm;
	gen I2_t_`k_year'_exp_L		=I2_t_x`k_year'*exp_L_IH_I2_SF12a_plm;
	
	
	label var I2_t_`k_year'_exp_I2		"IRI exp per I2 lane mile `k_year'";
	label var I2_t_`k_year'_exp_L   	"L exp per I2 lane mile `k_year'";
	
};//end year loop

	
	
gen urban=1 if  D_urban==1 | D_small_urban==1;
replace urban =0 if D_rural==1 ;
label var urban "Dummy=1 if urban" ;		
		
	
* time, for estimating trends;
gen time =year-1990;
gen time2 =time^2;
gen time3 =time^3;
label var time2 "\(time^2\)" ;
label var time3 "\(time^3\)" ;
*TIME TREND DUMMIES;


gen     periods=1 if  year>=1984&year<=1989;
replace periods=2 if  year>=1990&year<=1994;
replace periods=3 if  year>=1995&year<=1999;
replace periods=4 if  year>=2000&year<=2004;
replace periods=5 if  year>=2005&year<=2008;

tab year periods, m ;
label define periods 1 "1984-1989"
					2 "1990-1994"
					3 "1995-1999"
					4 "2000-2004"
					5 "2005-2008";

label values periods periods;


*** Gen state_year to cluster standar errors ;

egen state_year= group(state year);


*make I2_t * expenditure per mile variables;	

foreach I in Ids_t Iall_t I2_t{;
	gen `I'_exp_I2		=`I'*exp_IRI_IH_SF12a_plm;
	gen `I'_exp_L		=`I'*exp_L_IH_I2_SF12a_plm;
	gen `I'_app_I2  		=`I'*app_IH_I2_plm ;
	
	forvalues i=1(1)5{;
		gen `I'_L`i'_apport_I2=`I'*L`i'FHWA_apport_LW_r_plm;

	};	

	
	*LABEL FOR TEX OUPTU;
	label var `I'_exp_I2  		"\( \mathds{1}_{ist}(q)\imath^q_{st}\)";

};




	
# d cr ;	
